/*  -*- mode: c++; coding: utf-8; c-file-style: "stroustrup"; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "refcnt.hh"

reference_counter::reference_counter()
{
    refcnt=1;
}

reference_counter::~reference_counter()
{
    if(refcnt)
	ERROR("reference count (=%d) should be zero: %p", refcnt, this);
}

reference_counter *reference_counter::decref()
{
#   ifdef REFCNT_DEBUG
	DEBUG("%3d-- %p", refcnt, this);
#   endif

    if(!--refcnt)
    {
	// must delete object
	delete this;
	return NULL;
    }
    else if(refcnt<0) ERROR("reference count (=%d) below zero: %p", refcnt, this);
    return this;
}

void reference_counter::declared_local_or_member()
{
    refcnt=0;
}
